package io.rocketman.transport.netty.channel;

import io.netty.channel.Channel;
import io.rocketman.config.client.ClientMetaData;
import io.rocketman.config.server.ServerMetaData;
import io.rocketman.transport.channel.AbstractChannel;
import lombok.Getter;

import java.net.InetSocketAddress;
import java.util.concurrent.Future;

/**
 * @author He Peng
 * @create 2018-05-25 1:41
 * @update 2018-05-25 1:41
 * @updatedesc : 更新说明
 * @see
 */

@Getter
public class NettyChannel extends AbstractChannel {

    private Channel channel;

    public NettyChannel(boolean isClose, boolean isOpen, InetSocketAddress remoteAddress, InetSocketAddress localAddress, ServerMetaData serverMetaData, ClientMetaData clientMetaData, Channel channel) {
        super(isClose, isOpen, remoteAddress, localAddress, serverMetaData, clientMetaData);
        this.channel = channel;
    }

    @Override
    protected void doClose() {

    }

    @Override
    protected Future doSend(Object message) {
        return this.channel.writeAndFlush(message);
    }

    @Override
    public boolean isClose() {
        return false;
    }

    @Override
    public boolean isOpen() {
        return false;
    }

    @Override
    public InetSocketAddress getRemoteAddress() {
        return null;
    }

    @Override
    public InetSocketAddress getLocalAddress() {
        return null;
    }

    @Override
    public ServerMetaData getServerMetaData() {
        return null;
    }

    @Override
    public ClientMetaData getClientMetaData() {
        return null;
    }

    public static NettyChannelBuilder builder() {
        return new NettyChannelBuilder();
    }

    public static class NettyChannelBuilder {
        private boolean isClose;
        private boolean isOpen;
        private InetSocketAddress remoteAddress;
        private InetSocketAddress localAddress;
        private ServerMetaData serverMetaData;
        private ClientMetaData clientMetaData;
        private Channel channel;

        public NettyChannelBuilder isClose(boolean isClose) {
            this.isClose = isClose;
            return this;
        }

        public NettyChannelBuilder isOpen(boolean isOpen) {
            this.isOpen = isOpen;
            return this;
        }

        public NettyChannelBuilder remoteAddress(InetSocketAddress remoteAddress) {
            this.remoteAddress = remoteAddress;
            return this;
        }

        public NettyChannelBuilder localAddress(InetSocketAddress localAddress) {
            this.localAddress = localAddress;
            return this;
        }

        public NettyChannelBuilder serverMetaData(ServerMetaData serverMetaData) {
            this.serverMetaData = serverMetaData;
            return this;
        }

        public NettyChannelBuilder clientMetaData(ClientMetaData clientMetaData) {
            this.clientMetaData = clientMetaData;
            return this;
        }

        public NettyChannelBuilder channel(Channel channel) {
            this.channel = channel;
            return this;
        }

        public NettyChannel build() {
            return new NettyChannel(isClose , isOpen , remoteAddress , localAddress , serverMetaData ,
                    clientMetaData , channel);
        }
    }
}
